/**
 * 注册页面类
 * @author 李华顺<huacnlee@gmail.com>
 */
var Signup = {
    /**
     * 用于参数的 Signup 基本设置
     */
    settings: {
		/**
		 * Ajax提交地址
		 */
		postUrl : ""
	},

	/**
	 * 初始化 Signup 类
	 * @param {Object} settings
	 */
    init: function(settings){

        $.extend(Signup.settings, settings);
        settings = Signup.settings;

		var panelSignup = $("#signup");
		/* 让文本框改动的时候，去掉错误提示的样式 */
		$("input,textarea").change(function(){
			$(this).removeClass("error");
			Signup.showMessage();
		});
		/* 提交按钮绑定事件 */
		$("button.submit",panelSignup).click(function(){
			return Signup.post();
		});

    },

	/**
	 * 显示消息提示
	 * @param {Object} state ExecuteState 对象
	 */
	showMessage : function(state){

		var messageBox = $("#messageBox");
		if(state && state != undefined){

			messageBox.fadeOut();
			messageBox.text(state.message);
			if (state.success) {
				messageBox.css("color","green");
			}
			else {
				messageBox.css("color","red");
			}

			messageBox.fadeIn();

		}
		else{
			messageBox.text("");
		}

	},

	/**
	 * 验证表单是否输入正确
	 */
	checkForm : function(){

		var state = new Common.ExecuteState();

		var realname = $("#realname");
		var email = $("#email");
		var about = $("#about");

		if(realname.val() == ""){
			state.owner = realname;
			state.success = false;
			state.message = "姓名还未输入。";
			state.mark = "realname";

			return state;
		}

		if(! email.val().isEmail()){
			state.owner = email;
			state.success = false;
			state.message = "你的Email地址还未输入或格式不正确。";
			state.mark = "email";

			return state;
		}

		if(about.val().length < 20){
			state.owner = about;
			state.success = false;
			state.message = "“关于你”这项要求最少得填写20个字以上。";
			state.mark = "about";

			return state;
		}

		return state;
	},

	/**
	 * Ajax提交注冊信息
	 */
	post : function(){
		var state = new Common.ExecuteState();

		state  = Signup.checkForm();

		var realname = $("#realname");
		var email = $("#email");
		var about = $("#about");

		if(! state.success){
			state.owner.focus();
			state.owner.addClass("error");
			Signup.showMessage(state);
			return false;
		}

		$.ajax({
			url : Signup.settings.postUrl,
			type: Common.Settings.AJAX_TYPE,
			dataType: "json",
			data: {
				realname: realname.val(),
				email: email.val(),
				about: about.val()
			},
			success: function(state){
				if(! state.success){
					Signup.showMessage(state);
				}
				else{
					alert("您的注册申请我们已经接受！\r\n审核时间大概在一天左右，不管是否通过我们都将会发送Email通知你。\r\n请耐性等待，谢谢。")
					document.location.href = "/";
				}
			}
		});

		return false;

	}
}



